import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../common/api/live_audit_api.dart';
import '../../common/api/site_api.dart';
import '../../common/const/app_color_const.dart';
import '../../common/const/app_style_const.dart';
import '../../common/enum/btn_enum.dart';
import '../../common/type/data_list_container_type.dart';
import '../../common/util/common_util.dart';
import '../../widget/card_container.dart';
import '../../widget/data_list_container.dart';
import '../../widget/empty.dart';
import '../../widget/form_item.dart';
import '../../widget/gradient_btn.dart';
import '../../widget/refresh_list_indicator.dart';
import '../../widget/ripple_card_container.dart';

class LiveAuditPage extends StatefulWidget {
  const LiveAuditPage({Key? key}) : super(key: key);

  @override
  State<LiveAuditPage> createState() => LiveAuditPageState();
}

class LiveAuditPageState extends State<LiveAuditPage> {
  GlobalKey<DataListContainerState> dataListContainerStateKey =
      GlobalKey<DataListContainerState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  getData(DataListContainerQueryParam param) {
    return LiveAuditApi.getLiveAuditList({});
  }

  Widget buildTitle(context, item) {
    return LayoutBuilder(builder: (buildCtx, constraints) {
      return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        SizedBox(
          width: constraints.maxWidth / 2 + 40,
          child: Text(
            '${CommonUtil.formatMillisecondsSinceEpoch(item["addTime"] ?? 0, 'yyyy年MM月dd日 HH:mm')}申请',
            style: cardContainerNormalTitleStyle,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        GradientBtn(
            label: "详情",
            onTap: () {
              Navigator.of(context)
                  .pushNamed("/liveAuditDetail", arguments: {"id": item['id']});
            })
      ]);
    });
  }

  Widget listBodyBuilder(context, item) {
    return CardContainer(
        margin: const EdgeInsets.only(bottom: 10.0),
        titleBuilder: (context) => buildTitle(context, item),
        bodyPadding: const EdgeInsets.all(12.0),
        bodyBuilder: (c) => Container(
              color: lightGrayColor8,
              padding: const EdgeInsets.all(8.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LayoutBuilder(builder: (buildContext, constraints) {
                    return FormItem(
                      padding: const EdgeInsets.only(bottom: 6.0),
                      label: '手机号',
                      crossAxisAlignment: CrossAxisAlignment.start,
                      valueWidth: constraints.maxWidth - 60,
                      value: CommonUtil.valueEmptyToDefStr(item["contactTel"]),
                    );
                  }),
                  LayoutBuilder(builder: (buildContext, constraints) {
                    return FormItem(
                      padding: const EdgeInsets.only(bottom: 6.0),
                      label: '店铺类型',
                      crossAxisAlignment: CrossAxisAlignment.start,
                      valueWidth: constraints.maxWidth - 70,
                      value: CommonUtil.valueEmptyToDefStr(
                          item["rtoShopTypeName"]),
                    );
                  }),
                  LayoutBuilder(builder: (buildContext, constraints) {
                    return FormItem(
                      padding: const EdgeInsets.only(bottom: 6.0),
                      label: '渠道',
                      crossAxisAlignment: CrossAxisAlignment.start,
                      valueWidth: constraints.maxWidth - 50,
                      value:
                          CommonUtil.valueEmptyToDefStr(item["rtoChannelName"]),
                    );
                  }),
                ],
              ),
            ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("主播审核"),
      ),
      body: DataListContainer(
        key: dataListContainerStateKey,
        usePagination: false,
        getData: getData,
        listBodyBuilder: listBodyBuilder,
      ),
    );
  }
}
